package com.commerceerp.system.controller;

import com.commerceerp.system.entity.SysUser;
import com.commerceerp.system.service.LoginService;
import com.commerceerp.system.util.ApiResult;
import com.commerceerp.system.util.MD5Tools;
import com.commerceerp.system.util.StringUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/5/13/013.
 */
@RestController
@RequestMapping("/sys")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "登录请求")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "loginname", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "密码", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ApiResult doLogin(String loginname, String password){
        try {
            //密码进行加密处理  明文为  password
            String pawDES = MD5Tools.getSHA256StrJava(password);
            //从数据库获取对应用户名密码的用户
            SysUser systemUser = loginService.doLogin(loginname, pawDES);
            if (systemUser == null) {
                return new ApiResult(405,"用户名或密码错误");
            }
            String token = StringUtils.getUUID();
            loginService.saveToken(token,systemUser.getId());//保存在数据库
            Map<String,Object> map = new HashMap<>();
            map.put("token",token);
            map.put("user",systemUser);
            return new ApiResult(0,map,"登录成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(405,"用户名或密码错误");
        }
    }


    @ApiOperation(value = "注册")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "loginname", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "phone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "code", value = "短信验证码", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public ApiResult register(String loginname, String password,String phone,String code){

        return loginService.register(loginname,password,phone);
    }

    @ApiOperation(value = "退出登录")
    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public ApiResult logout(HttpServletRequest request){

        return loginService.logout(request.getHeader("token"));
    }

}
